<style rel="stylesheet/scss" lang="scss">


</style>

<template>
    <div>
        <div class="info_flow_content" @click="bodyEvent">
            <div class="menuNav">
                <p><i class="nav_index"></i><span v-for="(c, i) in menu_nav" :key="i"><i
                    class="el-icon-arrow-right"></i><em>{{c}}</em></span></p>
            </div>
            <div class="info_ad_search">
                <div class="info_a_s_content">
                    <div class="realTimeCitySearch">
                        <div class="block">
                            <span class="demonstration">选择日期</span>
                            <el-date-picker
                                v-model="dateValue"
                                type="daterange"
                                align="right"
                                placeholder="选择日期范围"
                                @change="dateChange"
                                format="yyyy / MM / dd"
                                :picker-options="$store.state.pickerOptions2">
                            </el-date-picker>
                        </div><!--
                        -->
                        <el-select v-model="company_items.model_value"
                                   :placeholder="company_items.placeHolder" class="con_select unBorderRadius"
                                   filterable @change="select('1',company_items.model_value)">
                            <el-option
                                v-for="item in company_items.options"
                                :key="item.code"
                                :label="item.name"
                                :value="item.code">
                            </el-option>
                        </el-select><!--
                        -->
                        <el-select v-model="pro_items.model_value"
                                   :placeholder="pro_items.placeHolder" class="con_select unBorderRadius">
                            <el-option
                                v-for="item in pro_items.options"
                                :key="item.productCode"
                                :label="item.productName"
                                :value="item.productCode">
                            </el-option>
                        </el-select><!--
                        --><el-button class="page_d_s_button" @click="pageInfo">查询</el-button>
                        <div class="dateBut">
                            <el-button type="primary" @click.stop="importData">导出数据</el-button>
                        </div>
                    </div>
                    <!-- <div class="dateSelect">
                        <div class="block">
                            <el-input v-model="searchvalue" class="page_d_select" placeholder="请输入上游渠道号"
                                      @keyup.enter.native="searchInfo()"></el-input>
                            <el-button class="page_d_s_button" @click="searchInfo()" style="margin-left:77px;">搜索
                            </el-button>
                        </div>
                    </div> -->
                </div>
            </div>
            <div class="info_ad_table">
                <div class="page_d_title">
                    <i style="visibility:hidden;"></i><span></span>
                    <div class="tableVisibleMenu">
                        <p class="visibleMenuBut" @click="changeCheckStatus($event)"></p>
                        <div class="visibleMenuSelect" v-if="visibleMenuSelectStatus" @click="preventDefault($event)">
                            <el-checkbox-group
                                v-model="checkedCities"
                                :min="1"
                                @change="checkedChange">
                                <el-checkbox v-for="city in cities" :label="city" :key="city">{{city}}</el-checkbox>
                            </el-checkbox-group>
                        </div>
                    </div>
                </div>
                <div class="tableDetail">
                    <el-table
                        v-loading="loading"
                        element-loading-text="拼命加载中"
                        :data="tableData"
                        border
                        stripe
                        style="width: 100%"
                    >
                        <el-table-column
                            v-for="(c, i) in table_column"
                            :key="i"
                            :fixed="c.fixed"
                            :sortable="c.sortable"
                            :prop="c.prop"
                            :label="c.label"
                            :formatter="c.format"
                            width=""
                            min-width="100"
                            v-if="c.visible">
                        </el-table-column>
                        <!--<el-table-column-->
                            <!--label="操作"-->
                            <!--width="100"-->
                            <!--min-width="100"-->
                            <!--class-name="exitBut">-->
                            <!--<template slot-scope="scope">-->
                                <!--<span v-if="scope.row.validStatus=='button'" @click="checkAllEvent"-->
                                      <!--class="r_span">{{checkAll ? '全选' : '取消全选'}}</span>-->
                                <!--<el-checkbox v-else v-model="scope.row.validStatus"></el-checkbox>-->
                            <!--</template>-->
                        <!--</el-table-column>-->
                    </el-table>
                </div>
            </div>
            <div class="con_pager">
                <div class="block">
                    <!--<span class="demonstration">直接前往</span>-->
                    <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page.sync="page.currentPage"
                        :page-sizes="page.sizes"
                        :page-size="page.limit"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="page.count">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
    export default {
        data() {
            return {
                fileList: [],
                visibleMenuSelectStatus: false,
                loading: false,
                checkAll: true,
                checkedCities: ['日期', '产品名称', '渠道号', '渠道名称', '预估激活', '结算激活', '留存率'],
                cities: ['日期', '产品名称', '渠道号', '渠道名称', '预估激活', '结算激活', '留存率'],
                citiesFilterVal:['dataTime','productName','productChannelCode','channelName','expectedAct','adjustmentAct','retention'],
                table_column: [
                    {fixed: false, prop: 'dataTime', label: '日期', sortable: false, visible: true},
                    {fixed: false, prop: 'productName', label: '产品名称', sortable: false, visible: true},
                    {fixed: false, prop: 'productChannelCode', label: '渠道号', sortable: false, visible: true},
                    {fixed: false, prop: 'channelName', label: '渠道名称', sortable: false, visible: true},
                    {fixed: false, prop: 'expectedAct', label: '预估激活', sortable: false, visible: true},
                    {fixed: false, prop: 'adjustmentAct', label: '结算激活', sortable: false, visible: true},
                    {fixed: false, prop: 'retention', label: '留存率', sortable: false, visible: true,format:function (row,col) {
                        if (row.dataTime != '总计') {
                            return row.retention + '%';
                        }
                        else {
                            return '';
                        }
                    }},
                ],
                dateValue: '',
                page: {
                    sizes: [20,50,100,200, 500],
                    offset: 1,
                    limit: 20,
                    count: 0,
                    currentPage: 1,
                },
                menu_nav: ['CPA数据管理', '实时数据'
                ],
                company_items: {
                    model_value: '',
                    placeHolder: '公司',
                    options: []
                },
                pro_items: {
                    model_value: '',
                    placeHolder: '产品',
                    options: []
                },
                params: {
                    type: '',
                    val: '',
                    startDate: '',
                    endDate: '',
                },
                tableData: [],
                idList: [],
                searchvalue: '',
                contractNo: '',
                isFirstEnter: false, // 是否第一次进入，默认false
            };
        },
        activated() {
            if (!this.$route.meta.isBack || this.isFirstEnter) {
                // 如果isBack是false，表明需要获取新数据，否则就不再请求，直接使用缓存的数据
                this.companyInfo();
            }
            // 恢复成默认的false，避免isBack一直是true，导致下次无法获取数据
            this.$route.meta.isBack = false;
            // 恢复成默认的false，避免isBack一直是true，导致每次都获取新数据
            this.isFirstEnter = false;
        },
        created() {
            this.isFirstEnter = true;
        },
        methods: {
            companyInfo() {
                /*公司*/
                this.$http({
                    method: 'get',
                    url: this.apiUrl.get_my_channel,
                }).then(res => {
                    console.log(res);
                    if (res.status == 200) {
                        var data = res.data;
                        this.company_items.options = data.channels;
                    } else {
//                        console.log('');
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            proInfo() {
                /*产品*/
                this.$http({
                    method: 'get',
                    url: this.apiUrl.list_product,
                    params: {
                        channelCode: this.company_items.model_value,
                    }
                }).then(res => {
                    console.log(res);
                    if (res.status == 200) {
                        var data = res.data;
                        this.pro_items.options = data.items;
                        this.pro_items.model_value = '';
//                        this.customerProductCode.model_value = '';
                    } else {
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            pageInfo() {
                if (!this.dateValue||!this.dateValue[0]) {
                    this.$message({
                        type: 'info',
                        message: '请选择日期'
                    });
                    return false;
                } else if (this.company_items.model_value == '') {
                    this.$message({
                        type: 'info',
                        message: '请选择公司'
                    });
                    return false;
                }
                /*列表*/
                this.$http({
                    method: 'get',
                    url: this.apiUrl.search_by_channel,
                    params: {
                        curPage: this.page.offset,
                        limit: this.page.limit,
                        sDataTime: this.params.startDate,
                        eDataTime: this.params.endDate,
                        channelCode: this.company_items.model_value,
                        productCode: this.pro_items.model_value,
                    }
                }).then(res => {
                    console.log(res);
                    if (res.status == 200) {
                        var data = res.data;
                        this.page.count = data.totalNums;
//                        this.checkAll = true;
                        this.tableData = data.list;
                        if (this.tableData.length > 0) {
                            this.tableData.unshift({
                                dataTime: '总计',
                                adjustmentAct: res.data.settlementActTotal,
                                validStatus: 'button'
                            });
                        }
//                        this.idList = [];
                    } else {
//                        console.log('');
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            searchInfo() {
                if (!this.dateValue||!this.dateValue[0]) {
                    this.$message({
                        type: 'info',
                        message: '请选择日期'
                    });
                    return false;
                } else if (this.searchvalue == '') {
                    this.$message({
                        type: 'info',
                        message: '请输入上游渠道号'
                    });
                    return false;
                }
                this.page.offset=1;
                /*列表*/
                this.$http({
                    method: 'get',
                    url: this.apiUrl.search,
                    params: {
                        type: 6,
                        curPage: this.page.offset,
                        limit: this.page.limit,
                        sDataTime: this.params.startDate,
                        eDataTime: this.params.endDate,
                        searchKey: this.searchvalue
                    }
                }).then(res => {
                    console.log(res);
                    if (res.status == 200) {
                        var data = res.data;
                        this.page.count = data.totalNums;
//                        this.checkAll = true;
                        this.tableData = data.list;
                        if (this.tableData.length > 0) {
                            this.tableData.unshift({
                                dataTime: '总计',
                                adjustmentAct: res.data.settlementActTotal,
                                validStatus: 'button'
                            });
                        }
//                        this.idList = [];
                    } else {
//                        console.log('');
                    }
                }).catch(error => {
                    console.log(error);
                });
            },
            select(type, val) {
                if (val != '' && typeof val != 'undefined') {
                    this.params.type = type;
                    this.params.val = val;
                    switch (type) {
                        case '1':
                            this.proInfo();
                            break;
                    }
                }
            },
            handleCurrentChange(val) {
                this.page.offset = val;
                this.pageInfo();
            },
            handleSizeChange(val) {
                this.page.limit = val;
                this.pageInfo();
            },
            checkedChange() {
                for (let item of this.table_column) {
                    var index = this.checkedCities.indexOf(item.label);
                    index >= 0 ? item.visible = true : item.visible = false;
                }
            },
            changeCheckStatus(e) {
                e.stopPropagation();
                this.visibleMenuSelectStatus = !this.visibleMenuSelectStatus;
            },
            bodyEvent() {
                this.visibleMenuSelectStatus = false;
            },
            preventDefault(e) {
                e.stopPropagation();
            },
            dateChange() {
                this.params.startDate = this.formatDateTime(this.dateValue[0], 'y-m-d');
                this.params.endDate = this.formatDateTime(this.dateValue[1], 'y-m-d');
            },
            /*全选事件*/
            checkAllEvent() {
                let flag = true;
                for (let item of this.tableData) {
                    if (item.validStatus != 'button') {
                        item.validStatus = this.checkAll;
                        flag = false;
                    }
                }
                if (flag)
                    return false;
                this.checkAll = !this.checkAll;
            },
            importData() {
                if (this.tableData.length == 0) {
                    this.$message({
                        type: 'info',
                        message: '请查询数据'
                    });
                    return false;
                }
                let tHeader = this.cities;
                let filterVal = this.citiesFilterVal;
                let importData = this.tableData;
                let fileName = [this.params.startDate,this.params.endDate, this.company_items.model_value, this.pro_items.model_value, '实时数据'].join('_');
                this.handleDownload(tHeader, filterVal, importData, fileName);
            },
        }
    };
</script>
